package com.yerbamateprimer.model;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.Set;

import org.apache.commons.lang.builder.ReflectionToStringBuilder;
import org.apache.commons.lang.builder.ToStringStyle;

import com.google.common.base.Preconditions;

/**
 * This class models a group of
 * people that usually drink 
 * yerba mate together.
 * 
 * @author Matias Liwski
 * @version 1.0
 */
public class Group implements Serializable{
	private static final long serialVersionUID = 8930264932987542184L;
	private Long id; 
	private String name;
	private Set<User> members;

	/**
	 * @param id The id of the group.
	 * @param name The name of the group.
	 * @param members The members of the group.
	 */
	public Group(Long id, String name, Collection<User> members) {
		this.members = new HashSet<User>();
		setId(id);
		setName(name);
		addAllMembers(members);
	}

	/**
	 * @return The id of the Group.
	 */
	public Long getId() {
		return id;
	}

	/**
	 * @param id The id of the Group, to set.
	 */
	public void setId(Long id) {
		Preconditions.checkArgument(id != null, "Couldn't set a null group id (id=" + id + ").");
		this.id = id;
	}

	/**
	 * @return The name of the Group.
	 */
	public String getName() {
		return name;
	}

	/**
	 * @param name The name of the Group, to set.
	 */
	public void setName(String name) {
		Preconditions.checkArgument(name != null && !name.equals(""), "Couldn't set a null or empty name (name=" + name + ").");
		this.name = name;
	}

	/**
	 * @return A safe copy, of the Group members.
	 */
	public Collection<User> getMembers() {
		return new ArrayList<User>(this.members);
	}

	/**
	 * @param members The members to add to the Group.
	 */
	public void addAllMembers(Collection<User> members) {
		Preconditions.checkArgument(members != null && !members.isEmpty(), "Couldn't add null or empty members (members=" + members + ").");
		this.members.addAll(members);
	}

	/**
	 * @param member The member to add to the Group.
	 */
	public void addMember(User member) {
		Preconditions.checkArgument(member != null, "Couldn't add a null member (member=" + member + ").");
		this.members.add(member);
	}

	/* (non-Javadoc)
	 * @see java.lang.Object#hashCode()
	 */
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((id == null) ? 0 : id.hashCode());
		result = prime * result + ((members == null) ? 0 : members.hashCode());
		result = prime * result + ((name == null) ? 0 : name.hashCode());
		return result;
	}

	/* (non-Javadoc)
	 * @see java.lang.Object#equals(java.lang.Object)
	 */
	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Group other = (Group) obj;
		if (id == null) {
			if (other.id != null)
				return false;
		} else if (!id.equals(other.id))
			return false;
		if (members == null) {
			if (other.members != null)
				return false;
		} else if (!members.equals(other.members))
			return false;
		if (name == null) {
			if (other.name != null)
				return false;
		} else if (!name.equals(other.name))
			return false;
		return true;
	}

	/* (non-Javadoc)
	 * @see java.lang.Object#toString()
	 */
	@Override
	public String toString() {
		return ReflectionToStringBuilder.toString(this, ToStringStyle.SHORT_PREFIX_STYLE, false, false);
	}
}